// Copyright 2022 jeff.li. and/or its affiliates.

#include <tbir/runtime/func_registry_names_io.h>

#include <sstream>

#include <tbir/runtime/container/string_view.h>
#include <tbir/runtime/logging.h>

namespace tbir::runtime {

    String GenerateFuncRegistryNames(const std::vector<String> &function_names) {
        std::stringstream ss;
        ss << std::to_string(function_names.size()) << '\0';
        for (auto &f : function_names) {
            ss << f << '\0';
        }

        return ss.str();
    }

    std::vector<string_view> ReadFuncRegistryNames(const char *names) {
        string_view num_str(names);
        uint64_t num = std::strtoull(num_str.data(), nullptr, 10);
        const char *reg_name_ptr = names + num_str.size() + 1;
        std::vector<string_view> result;

        result.reserve(num);
        // NOTE: reg_name_ptr starts at index 1 to skip num_funcs.
        for (size_t i = 0; i < num; ++i) {
            result.emplace_back(string_view(reg_name_ptr));
            reg_name_ptr += result.back().size() + 1;
        }
        return result;
    }

    int LookupFuncRegistryName(const char *names, string_view target) {
        size_t num = (unsigned char) names[0];
        const char *reg_name_ptr = names + 1;
        // NOTE: reg_name_ptr starts at index 1 to skip num_funcs.
        for (size_t i = 0; i < num; ++i) {
            string_view reg_name(reg_name_ptr);
            if (reg_name == target) {
                return i;
            }
            reg_name_ptr += reg_name.size() + 1;
        }
        return -1;
    }

}  // namespace tbir::runtime
